package com.qst.service.ssm;

import com.qst.ExamException;
import com.qst.entity.User;
import com.qst.mapper.UserMapper;
import com.qst.service.UserAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAdminServiceSsmImpl implements UserAdminService  {

    private final UserMapper userMapper;

    @Autowired
    public UserAdminServiceSsmImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public List<User> findUsers() {
        return userMapper.findAll();
    }

    @Override
    public User findById(int id) {
        return userMapper.findUserById(id);
    }

    @Override
    public void deleteById(int id) {
        userMapper.delUserById(id);
    }

    @Override
    public void saveUser(User user) {
       User tempUser = userMapper.findByLogin(user.getLogin());
        if(tempUser != null && user.getLogin().equals(tempUser.getLogin())){
            throw new ExamException("登录名已存在");
        }
        userMapper.insertUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void resetPassword(int id) {
        User userById = findById(id);
        userById.setPasswd(User.PASSWORD);
        userMapper.resetPwdbyId(userById);
    }
}
